package com.fortune.service.impl;

import com.fortune.dao.CReturnDepositDao;
import com.fortune.dao.DriverReturnDepositDao;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.service.DriverReturnDepositService;
import com.fortune.vo.cReturnDeposit.CReturnDepositDetail;
import com.fortune.vo.cReturnDeposit.DetailCReturn;
import com.fortune.vo.driverReturnDeposit.ConfirmBox;
import com.fortune.vo.driverReturnDeposit.DricerConfirmBoxDto;
import com.fortune.vo.driverReturnDeposit.DriverReturnDepositDetail;
import com.fortune.vo.driverReturnDeposit.DriverReturnDepositInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2018/5/24 0024.
 */
@Service
public class DriverReturnDepositServiceImpl implements DriverReturnDepositService{
    @Autowired
    private DriverReturnDepositDao driverReturnDepositDao;
    @Override
    public List<DriverReturnDepositInfo> findDriverReturnDepositList(String did,String id) {
        List<DriverReturnDepositInfo> list = driverReturnDepositDao.findDriverReturnDepositList(did,id);
        for(DriverReturnDepositInfo driverReturnDepositInfo : list){
            BigDecimal aa = new BigDecimal(0);
            List<DriverReturnDepositDetail> oo = driverReturnDepositDao.findTotalPrice(did,driverReturnDepositInfo.getId());
                for(DriverReturnDepositDetail depositDetail : oo){
                   aa = aa.add(depositDetail.getTotalPrice());
                }
                driverReturnDepositInfo.setTotalPrice(aa);
        }
        return list;
    }

    @Override
    public DriverReturnDepositDetail findDriverReturnDepositDetail(String id, String clientId, String did) {
        DriverReturnDepositDetail driverReturnDepositDetail = driverReturnDepositDao.findClient(clientId);
        List<DetailCReturn> list = driverReturnDepositDao.findDriverReturnDepositDetail(id,clientId,did);
        BigDecimal aa = new BigDecimal(0);
        for (DetailCReturn detailCReturn : list){
            aa =aa.add(detailCReturn.getDprice());
        }
        driverReturnDepositDetail.setTotalPrice(aa);
        driverReturnDepositDetail.setList(list);
        return driverReturnDepositDetail;
    }

    @Override
    @Transactional
    public int driverConfirmationBox(DricerConfirmBoxDto dto, String did,String driverId) {
        int success = driverReturnDepositDao.updateBase(dto.getId(),dto.getClientId(),did,driverId);
        if(success > 0){
            List<ConfirmBox> list = dto.getBox();
            for(ConfirmBox confirmBox : list){
                int num = driverReturnDepositDao.driverConfirmationBox(dto.getClientId(),dto.getId(),confirmBox,did);
                if(num < 1){
                    throw new UpdateInnerErrorException("编辑失败");
                }
            }
        }
        return success;
    }
}
